package com.start;


import com.start.common.api.Result;
import com.start.common.exceptions.StartBusException;
import com.start.common.exceptions.UnAuthorizedException;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.resource.NoResourceFoundException;

/**
 * 全局异常信息捕捉
 */
@Slf4j
@Component
@ControllerAdvice
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result<?> exceptionHandler(Exception e) {
        e.printStackTrace();
        Result<?> deadly = Result.deadly();
        deadly.setMessage(e.getMessage());
        return deadly;
    }

    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<?> RuntimeExceptionHandler(RuntimeException e) {
        e.printStackTrace();
        return Result.fail(e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(value = StartBusException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<?> BusinessExceptionHandler(StartBusException e) {
        if (e.isPrintStackTrace()) {
            e.printStackTrace();
        }
        return Result.fail(e.getMessage());
    }


    @ResponseBody
    @ExceptionHandler(value = AccessDeniedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result<?> AccessDeniedExceptionHandler(AccessDeniedException e) {
        e.printStackTrace();
        return Result.fail403();
    }

    @ResponseBody
    @ExceptionHandler(value = UnAuthorizedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result<?> UnAuthorizedExceptionHandler(UnAuthorizedException e) {
        e.printStackTrace();
        return Result.fail401("用户未登录");
    }

    @ResponseBody
    @ExceptionHandler(value = UsernameNotFoundException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result<?> UsernameNotFoundExceptionHandler(UsernameNotFoundException e) {
        e.printStackTrace();
        return Result.fail("用户名或密码错误");
    }

    @ResponseBody
    @ExceptionHandler(value = NoResourceFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Result<?> UsernameNotFoundExceptionHandler(NoResourceFoundException e) {
        e.printStackTrace();
        return Result.fail404("资源路径不存在：" + e.getResourcePath());
    }

    @ResponseBody
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public Result<?> HttpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e) {
        e.printStackTrace();
        return Result.fail(e.getMethod());
    }


    @ResponseBody
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ExceptionHandler(value = SchedulerException.class)
    public Result<?> SchedulerExceptionHandler(SchedulerException e) {
        e.printStackTrace();
        return Result.fail("定时任务异常");
    }

}
